import 'package:flutter/material.dart';
class StarRating extends StatefulWidget{
  final double rating;
  final double maxRating;
  final Widget unSelectedImage;
  final Widget selectedImage;

  final int count;
  final double size;

  final Color unSelectedColor;
  final Color selectedColor;

StarRating({
  this.rating,
  this.maxRating=10,

  this.size=30,

  this.unSelectedColor=const Color(0xffbbbbbb),

  this.selectedColor=const Color(0xff0aa46),

   Widget selectedImage,

   Widget unSelectedImage,

  this.count=5,  
}):unSelectedImage = unSelectedImage ?? Icon(Icons.star,size:size,color: unSelectedColor,),selectedImage = selectedImage ?? Icon(Icons.star,size:size,color:selectedColor);

@override
_HYStarRatingState createState() =>_HYStarRatingState();
}
class MyRectClipper extends CustomClipper<Rect>{
  final double width;

  MyRectClipper({
    this.width
  });

  @override
  Rect getClip(Size size) {
    return Rect.fromLTRB(0, 0, width, size.height);
  }

  @override
  bool shouldReclip(MyRectClipper oldClipper) {
    return width != oldClipper.width;
  }
}

class _HYStarRatingState extends State<StarRating>{

@override
  Widget build(BuildContext context){
    return Container(

      child: Stack(
        children: [
          Row(children: getUnSelectImage(),mainAxisSize: MainAxisSize.max,),
          Row(children: getSelectImage(),mainAxisSize: MainAxisSize.max,)
        ],
      ),
    );
  }

  List<Widget> getUnSelectImage(){
    return List.generate(widget.count, (index)=>widget.unSelectedImage);
  }

List getSelectImage(){
  //1、计算star个数和剩余比例
  double oneValue=widget.maxRating/widget.count;

  int entireCount=(widget.rating/oneValue).floor();

  double leftValue=widget.rating-entireCount* oneValue;

  double leftRatio=leftValue/oneValue;

  //2、获取start
  List<Widget> selectedImages=[];
  for(int i=0;i<entireCount;i++){
selectedImages.add(widget.selectedImage);

  }

  //3、计算
  Widget leftStar=ClipRect(
    clipper: MyRectClipper(width:leftRatio*widget.size),
    child: widget.selectedImage,
  );
  selectedImages.add(leftStar);
  return selectedImages;
}
}
